package com.cli.springboot_rabbitmq.provider;

import com.cli.springboot_rabbitmq.config.RabbitMQService;
import com.cli.springboot_rabbitmq.model.User;
import com.cli.springboot_rabbitmq.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021/2/26 10:55
 * @Version 1.0
 **/
@Service
public class TopicProviderService {

    @Autowired
    private RabbitMQService rabbitMQService;

    public String sendDirectMessage() {
        Map<String, Object> map = DataUtils.generateData();
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMQService.messageDeliver("topicExchange", "topic.MAN", map);
        return "ok";
    }

    /**
     * 测试消息转换器的作用,topic.woman 接受的要是一个HashMap类型的消息，如果不是String类型就会报错
     *
     * @return
     */
    public String sendDirectMessageRight() {
        Map<String, Object> map = DataUtils.generateData();
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMQService.messageDeliver("topicExchange", "topic.WOMAN", map);
        return "ok";
    }

    /**
     * 这个时候将会报错，因为使用的消息转换器是 Jackson2JsonMessageConverter  发送信息的类型一定要和接受信息的类型一致否则将会报错
     * 而这里发送信息的是String 类型 而接受类型使用的 确是 HashMap 所以会报错
     *
     * @return
     */
    public String sendDirectMessageError() {
        String msg = "this is a right string message";
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMQService.messageDeliver("topicExchange", "topic.WOMAN", msg);
        return "ok";
    }

    public String sendDirectMessageUser() {
        User user = new User("hans",24);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMQService.messageDeliver("topicExchange", "topic.WOMAN", user);
        return "ok";
    }


}
